﻿using Domain.Entities;
using Domain.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.RoleApp
{
    public class RoleAppService : IRoleAppService
    {

        //用户管理仓储接口
        private readonly IRoleRepository _rolerepository;
        private readonly IRoleMenuRepository _rolemenurepository;
        private readonly IMenuRepository _menurepository;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        /// <param name="RoleRepository">仓储对象</param>
        public RoleAppService(IRoleRepository RoleRepository, IRoleMenuRepository RoleMenuRepository, IMenuRepository MenuRepository)
        {
            _rolerepository = RoleRepository;
            _rolemenurepository = RoleMenuRepository;
            _menurepository = MenuRepository;
        }

        public Role GetById(Int32 id)
        {
            return _rolerepository.GetById(id);
        }

        public List<Role> GetAll(string key)
        {
            return _rolerepository.GetAll(key).ToList();
        }

        public List<Role> GetPageList(string SearchKeyWords, int PageSize, int PageCurrent, string OrderField, bool OrderDirection = true)
        {

            return _rolerepository.GetPageList(SearchKeyWords, PageSize, PageCurrent, OrderField, OrderDirection).ToList();
        }
        public bool AddOrUpdate(Role model)
        {
            return _rolerepository.AddOrUpdate(model);
        }

        public bool DeleteById(int id)
        {
            return _rolerepository.DeleteById(id);
        }

        /// <summary>
        /// 获取权限菜单json string
        /// </summary>
        /// <param name="RoleId">登录用户的角色ID</param>
        /// <param name="menurootid">根目录ID</param>
        /// <returns></returns>
        public string GetRoleMenuHtml(int RoleId, int menurootid)
        {
            StringBuilder menuhtml = new StringBuilder();
            List<RoleMenu> list = _rolemenurepository.GetAll(RoleId.ToString()).Where(i => i.Disable == 1).ToList();//获取角色对应菜单[可用]权限
            List<Menu> firstlist = _menurepository.GetAll(menurootid.ToString()).Where(i => i.Disable == 1).OrderBy(i => i.Menu_rank).ToList();//根据一级目录ID获取二级目录[可用]菜单,并排序
            menuhtml.Append("[");
            int y = 0;
            foreach (Menu menu1 in firstlist)
            {
                if (list.Where(i => i.Menu_Id == menu1.Id && i.Disable == 1).Count() > 0)//判断对应菜单，角色是否有 可用权限
                {
                    menuhtml.Append("{");
                    menuhtml.Append("\"name\": \"" + menu1.Menu_name + "\",");
                    menuhtml.Append("\"children\": [");

                    List<Menu> secondlist = _menurepository.GetAll(menu1.Id.ToString()).Where(i => i.Disable == 1).OrderBy(i => i.Menu_rank).ToList();//根据二级目录ID获取三级目录[可用]菜单,并排序
                    int x = 0;
                    foreach (Menu menu2 in secondlist)
                    {
                        if (list.Where(i => i.Menu_Id == menu2.Id && i.Disable == 1).Count() > 0)//判断对应菜单，角色是否有 可用权限
                        {
                            menuhtml.Append("{");
                            menuhtml.Append("\"id\": \"" + menu2.Menu_idname + "\",");
                            menuhtml.Append("\"name\": \"" + menu2.Menu_name + "\",");
                            menuhtml.Append("\"target\": \"" + menu2.Menu_target + "\",");
                            menuhtml.Append("\"url\": \"" + menu2.Menu_url + "\"");
                            menuhtml.Append("}");
                            menuhtml.Append(",");
                            x++;
                        }

                    }
                    if (x > 0)
                        menuhtml.Remove(menuhtml.ToString().LastIndexOf(','), 1);
                    menuhtml.Append("]");
                    menuhtml.Append("}");
                    menuhtml.Append(",");
                    y++;
                }

            }
            if (y > 0)
                menuhtml.Remove(menuhtml.ToString().LastIndexOf(','), 1);
            menuhtml.Append("]");
            return menuhtml.ToString();
        }

        /// <summary>
        /// 根据角色ID获取可用根目录
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public string GetRoleRootMenu(int RoleId)
        {
            StringBuilder menuhtml = new StringBuilder();
            List<RoleMenu> list = _rolemenurepository.GetAll(RoleId.ToString()).Where(i => i.Disable == 1).ToList();//获取角色对应菜单[可用]权限
            List<Menu> rootlist = _menurepository.GetAll("0").Where(i => i.Disable == 1).OrderBy(i => i.Menu_rank).ToList();//获取根目录[可用]菜单,并排序
            int x = 0;
            foreach (Menu menu in rootlist)
            {
                if (x == 0)
                    menuhtml.Append("<li class=\"active\">");
                else
                    menuhtml.Append("<li>");

                menuhtml.Append("    <a href=\"/home/homeMenu/" + menu.Id.ToString() + "\" data-toggle=\"sidenav\" data-id-key=\"targetid\">"+menu.Menu_name+"</a>");
                menuhtml.Append("</li>");
                x++;
            }
            return menuhtml.ToString();
        }
    }
}
